/* Copyright 2011 Jonathan Komorek
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jkomorek.eurl.selenium;

import org.apache.commons.lang.StringEscapeUtils;
import org.jkomorek.eurl.selenium.commands.*;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

/**
 *
 * @author Jonathan Komorek
 */
public abstract class Step {

    protected Script script;
    protected String target;
    protected String value;

    public abstract void execute(ScriptRunner scriptRunner);

    public static Step createStep(Script script, String command, String target, String value) {
        System.out.println("Creating step with command["+command+"] target["+target+"] value["+value+"]");

        Step step;

        if (command == null) throw new RuntimeException("Cannot create step with null command");
        else if ("".equals(command)) throw new RuntimeException("Cannot create step with empty command");
        else if ("open".equals(command)) step = new Open();
        else if ("click".equals(command)) step = new Click();
        else if ("clickAndWait".equals(command)) step = new ClickAndWait();
        else if ("type".equals(command)) step = new Type();
        else if ("assertTextPresent".equals(command)) step = new AssertTextPresent();
        else throw new RuntimeException("Unsupported command["+command+"]");

        step.setScript(script);
        step.setTarget(target);
        step.setValue(value);
        return step;
    }

    protected WebElement findTarget(ScriptRunner scriptrunner) {
        System.out.println("Attempting to find target from ["+target+"]");
        int equalsLocation = target.indexOf('=');
        String targetBy = target.substring(0, equalsLocation);
        String targetValue = target.substring(equalsLocation+1, target.length());

        targetValue = StringEscapeUtils.unescapeHtml(targetValue);

        By by;
        if( target == null) throw new RuntimeException("Unsupprted target type ["+targetBy+"] in target ["+target+"]");
        else if("id".equals(targetBy)) by = By.id(targetValue);
        else if("css".equals(targetBy)) by = By.cssSelector(targetValue);
        else if("link".equals(targetBy)) by = By.linkText(targetValue);
        else if("name".equals(targetBy)) by = By.name(targetValue);
        else throw new RuntimeException("Unsupprted target type ["+targetBy+"] in target ["+target+"]");

        return scriptrunner.getWebDriver().findElement(by);

    }



    public String getTarget() {
        return target;
    }

    public void setTarget(String target) {
        this.target = target;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public Script getScript() {
        return script;
    }

    public void setScript(Script script) {
        this.script = script;
    }

}
